/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package eypweb_bsc;

import java.io.*;
import java.util.ArrayList;
import java.sql.*;
import javafx.collections.ObservableList;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

/**
 *
 * @author Kohalmi Istvan
 */

// XML fajlkezelest megvalosito osztaly
public class XMLHandler{
    
    // egyelore nincs szukseg peldanyositasra
    public XMLHandler(){}
    
    // devizak exportalasa
    public static File exportCurrency(ObservableList<Currency> list){
        Director director = Director.getInstance();
        Stage stage = director.getStage();
        FileChooser fc = new FileChooser();
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Extensible Markup Language (*.xml)", "*.xml");
        fc.getExtensionFilters().add(filter);
        fc.setTitle("XML exportálása...");
        fc.setInitialDirectory(File.listRoots()[0]);
        File exportXML = fc.showSaveDialog(stage);
        try{
            // kimeneti adatfolyam inditasa
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportXML),"utf-8"));
            // XML fejlec feljegyzese
            out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            // kollekcio megnyitasa
            out.append("<devizak>\n");
            // a lista minden tagjara
            for (Currency cur : list)
            {
                // listaelem megnyitasa
                out.append("\t<deviza>\n");
                // listaelem mezoinek kiirasa
                out.append("\t\t<megnev>"+cur.getName()+"</megnev>\n");
                out.append("\t\t<egyseg>"+cur.getUnit()+"</egyseg>\n");
                out.append("\t\t<hatalytol>"+cur.getStartDate()+"</hatalytol>\n");
                out.append("\t\t<hatalyig>"+cur.getEndDate()+"</hatalyig>\n");
                // listaelem lezarasa
                out.append("\t</deviza>\n");
            }
            // a kollekcio lezarasa
            out.append("</devizak>");
            // adatfolyam lezarasa
            out.close();
        }
        catch(Exception e)
        {
            System.err.println("Írási hiba \n" + e.getMessage());
        }
        return exportXML;
    }
    
    // eszkozcsoportok exportalasa
    public static File exportInstrumentGroup(ObservableList<InstrumentGroup> list){
        Director director = Director.getInstance();
        Stage stage = director.getStage();
        FileChooser fc = new FileChooser();
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Extensible Markup Language (*.xml)", "*.xml");
        fc.getExtensionFilters().add(filter);
        fc.setTitle("XML exportálása...");
        fc.setInitialDirectory(File.listRoots()[0]);
        File exportXML = fc.showSaveDialog(stage);
        try{
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportXML),"utf-8"));
            out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            out.append("<eszkoz_csoportok>\n");
            for (InstrumentGroup iGrp : list)
            {
                out.append("\t<eszkoz_csoport>\n");
                out.append("\t\t<megnev>"+iGrp.getName()+"</megnev>\n");
                out.append("\t\t<hatalytol>"+iGrp.getStartDate()+"</hatalytol>\n");
                out.append("\t\t<hatalyig>"+iGrp.getEndDate()+"</hatalyig>\n");
                out.append("\t</eszkoz_csoport>\n");
            }
            out.append("</eszkoz_csoportok>");
            out.close();
        }
        catch(Exception e)
        {
            System.err.println("Írási hiba \n" + e.getMessage());
        }
        return exportXML;
    }
    
    // befektetes csoportok exportalasa
    public static File exportInvestmentGroup(ObservableList<InvestmentGroup> list){
        Director director = Director.getInstance();
        Stage stage = director.getStage();
        FileChooser fc = new FileChooser();
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Extensible Markup Language (*.xml)", "*.xml");
        fc.getExtensionFilters().add(filter);
        fc.setTitle("XML exportálása...");
        fc.setInitialDirectory(File.listRoots()[0]);
        File exportXML = fc.showSaveDialog(stage);
        try{
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportXML),"utf-8"));
            out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            out.append("<befektetes_csoportok>\n");
            for (InvestmentGroup iGrp : list)
            {
                out.append("\t<befektetes_csoport>\n");
                out.append("\t\t<megnev>"+iGrp.getName()+"</megnev>\n");
                out.append("\t\t<megjegyzes>"+iGrp.getNote()+"</megjegyzes>\n");
                out.append("\t\t<hatalytol>"+iGrp.getStartDate()+"</hatalytol>\n");
                out.append("\t\t<hatalyig>"+iGrp.getEndDate()+"</hatalyig>\n");
                out.append("\t</befektetes_csoport>\n");
            }
            out.append("</befektetes_csoportok>");
            out.close();
        }
        catch(Exception e)
        {
            System.err.println("Írási hiba \n" + e.getMessage());
        }
        return exportXML;
    }
    
    // portfoliok exportalasa
    public static File exportPortfolio(ObservableList<Portfolio> list){
        Director director = Director.getInstance();
        Stage stage = director.getStage();
        FileChooser fc = new FileChooser();
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Extensible Markup Language (*.xml)", "*.xml");
        fc.getExtensionFilters().add(filter);
        fc.setTitle("XML exportálása...");
        fc.setInitialDirectory(File.listRoots()[0]);
        File exportXML = fc.showSaveDialog(stage);
        try{
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportXML),"utf-8"));
            out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            out.append("<portfoliok>\n");
            for (Portfolio port : list)
            {
                out.append("\t<portfolio>\n");
                out.append("\t\t<megnev>"+port.getName()+"</megnev>\n");
                out.append("\t\t<hatalytol>"+port.getStartDate()+"</hatalytol>\n");
                out.append("\t\t<hatalyig>"+port.getEndDate()+"</hatalyig>\n");
                out.append("\t</portfolio>\n");
            }
            out.append("</portfoliok>");
            out.close();
        }
        catch(Exception e)
        {
            System.err.println("Írási hiba \n" + e.getMessage());
        }
        return exportXML;
    }
    
    // ertekpaporok exportalasa
    public static File exportSecurities(ObservableList<Securities> list){
        Director director = Director.getInstance();
        Stage stage = director.getStage();
        FileChooser fc = new FileChooser();
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Extensible Markup Language (*.xml)", "*.xml");
        fc.getExtensionFilters().add(filter);
        fc.setTitle("XML exportálása...");
        fc.setInitialDirectory(File.listRoots()[0]);
        File exportXML = fc.showSaveDialog(stage);
        try{
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportXML),"utf-8"));
            out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            out.append("<ertekpapirok>\n");
            for (Securities secr : list)
            {
                out.append("\t<ertekpapir>\n");
                out.append("\t\t<isin>"+secr.getISIN()+"</isin>\n");
                out.append("\t\t<megnev>"+secr.getName()+"</megnev>\n");
                out.append("\t\t<ecs>"+secr.getInstrument()+"</ecs>\n");
                out.append("\t\t<bcs>"+secr.getInvestment()+"</bcs>\n");
                out.append("\t\t<deviza>"+secr.getCurrency()+"</deviza>\n");
                out.append("\t\t<alapcimlet>"+secr.getDenomination()+"</alapcimlet>\n");
                out.append("\t\t<71OJ>"+secr.getTIOJ()+"</71OJ>\n");
                out.append("\t\t<71OJ_C>"+secr.getTIOJ_C()+"</71OJ_C>\n");
                out.append("\t\t<71OK>"+secr.getTIOK()+"</71OK>\n");
                out.append("\t\t<lejarat>"+secr.getExpiry()+"</lejarat>\n");
                out.append("\t\t<hatalytol>"+secr.getStartDate()+"</hatalytol>\n");
                out.append("\t\t<hatalyig>"+secr.getEndDate()+"</hatalyig>\n");
                out.append("\t\t<alap>"+secr.getAccountBase()+"</alap>\n");
                out.append("\t</ertekpapir>\n");
            }
            out.append("</ertekpapirok>");
            out.close();
        }
        catch(Exception e)
        {
            System.err.println("Írási hiba \n" + e.getMessage());
        }
        return exportXML;
    }
    
    // tranzakciotipusok exportalasa
    public static File exportTransaction(ObservableList<Transaction> list){
        Director director = Director.getInstance();
        Stage stage = director.getStage();
        FileChooser fc = new FileChooser();
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Extensible Markup Language (*.xml)", "*.xml");
        fc.getExtensionFilters().add(filter);
        fc.setTitle("XML exportálása...");
        fc.setInitialDirectory(File.listRoots()[0]);
        File exportXML = fc.showSaveDialog(stage);
        try{
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportXML),"utf-8"));
            out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            out.append("<tranzakciok>\n");
            for (Transaction tran : list)
            {
                out.append("\t<tranzakcio>\n");
                out.append("\t\t<megnev>"+tran.getName()+"</megnev>\n");
                out.append("\t\t<delta_db>"+tran.getDeltaVolume()+"</delta_db>\n");
                out.append("\t\t<bsze_csok>"+tran.getDecrAV()+"</bsze_csok>\n");
                out.append("\t\t<kamat>"+tran.getSepInterest()+"</kamat>\n");
                out.append("\t\t<osztalek>"+tran.getSepDividend()+"</osztalek>\n");
                out.append("\t\t<piaci_arf_ny>"+tran.getMktRateGain()+"</piaci_arf_ny>\n");
                out.append("\t\t<deviza_arf_ny>"+tran.getCurRateGain()+"</deviza_arf_ny>\n");
                out.append("\t\t<hatalytol>"+tran.getStartDate()+"</hatalytol>\n");
                out.append("\t\t<hatalyig>"+tran.getEndDate()+"</hatalyig>\n");
                out.append("\t</tranzakcio>\n");
            }
            out.append("</tranzakciok>");
            out.close();
        }
        catch(Exception e)
        {
            System.err.println("Írási hiba \n" + e.getMessage());
        }
        return exportXML;
    }
    
    // vagyonkezelok exportalasa
    public static File exportTrustee(ObservableList<Trustee> list){
        Director director = Director.getInstance();
        Stage stage = director.getStage();
        FileChooser fc = new FileChooser();
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Extensible Markup Language (*.xml)", "*.xml");
        fc.getExtensionFilters().add(filter);
        fc.setTitle("XML exportálása...");
        fc.setInitialDirectory(File.listRoots()[0]);
        File exportXML = fc.showSaveDialog(stage);
        try{
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportXML),"utf-8"));
            out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            out.append("<vagyonkezelok>\n");
            for (Trustee trst : list)
            {
                out.append("\t<vagyonkezelo>\n");
                out.append("\t\t<nev>"+trst.getName()+"</nev>\n");
                out.append("\t\t<r_nev>"+trst.getShortName()+"</r_nev>\n");
                out.append("\t\t<irsz>"+trst.getZip()+"</irsz>\n");
                out.append("\t\t<varos>"+trst.getCity()+"</varos>\n");
                out.append("\t\t<cim>"+trst.getAddress()+"</cim>\n");
                out.append("\t\t<tel>"+trst.getTel()+"</tel>\n");
                out.append("\t\t<mobil>"+trst.getMobile()+"</mobil>\n");
                out.append("\t\t<fax>"+trst.getFax()+"</fax>\n");
                out.append("\t\t<mail>"+trst.getMail()+"</mail>\n");
                out.append("\t\t<kapcsolat>"+trst.getConnection()+"</kapcsolat>\n");
                out.append("\t\t<beosztas>"+trst.getAssignment()+"</beosztas>\n");
                out.append("\t\t<hatalytol>"+trst.getStartDate()+"</hatalytol>\n");
                out.append("\t\t<hatalyig>"+trst.getEndDate()+"</hatalyig>\n");
                out.append("\t</vagyonkezelo>\n");
            }
            out.append("</vagyonkezelok>");
            out.close();
        }
        catch(Exception e)
        {
            System.err.println("Írási hiba \n" + e.getMessage());
        }
        return exportXML;
    }
    
    // muveletek exportalasa
    public static File exportOperation(ObservableList<Inventory> list){
        Director director = Director.getInstance();
        Stage stage = director.getStage();
        FileChooser fc = new FileChooser();
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Extensible Markup Language (*.xml)", "*.xml");
        fc.getExtensionFilters().add(filter);
        fc.setTitle("XML exportálása...");
        fc.setInitialDirectory(File.listRoots()[0]);
        File exportXML = fc.showSaveDialog(stage);
        try{
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportXML),"utf-8"));
            out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            out.append("<eszkozok>\n");
            for (Inventory inv : list)
            {
                out.append("\t<eszkoz>\n");
                out.append("\t\t<id_vetel>"+inv.getPurchaseId()+"</id_vetel>\n");
                out.append("\t\t<b_dat>"+inv.getAcquisitionDate()+"</b_dat>\n");
                out.append("\t\t<mennyiseg>"+inv.getQuantity()+"</mennyiseg>\n");
                out.append("\t\t<nevertek>"+inv.getNominal()+"</nevertek>\n");
                out.append("\t\t<b_ertek>"+inv.getAcquisitionValue()+"</b_ertek>\n");
                out.append("\t\t<p_ar>"+inv.getMarketValue()+"</p_ar>\n");
                out.append("\t\t<ek_kamat>"+inv.getDerecogniseInterest()+"</ek_kamat>\n");
                out.append("\t\t<ek_oszt>"+inv.getDerecogniseDividend()+"</ek_oszt>\n");
                out.append("\t\t<ek_epr>"+inv.getDerecogniseRateDelta()+"</ek_epr>\n");
                out.append("\t\t<ek_dev>"+inv.getDerecogniseCurrencyRateDelta()+"</ek_dev>\n");
                out.append("\t\t<ugyletszam>"+inv.getSerial()+"</ugyletszam>\n");
                out.append("\t\t<elsz_alap>"+inv.getAccountedBase()+"</elsz_alap>\n");
                out.append("\t\t<konyv_ertek>"+inv.getAccountedValue()+"</konyv_ertek>\n");
                out.append("\t\t<vetel_kamat>"+inv.getAccumulatedInterestAccountedPrice()+"</vetel_kamat>\n");
                out.append("\t\t<real_kamat>"+inv.getRealisedInterest()+"</real_kamat>\n");
                out.append("\t\t<real_oszt>"+inv.getRealisedDividend()+"</real_oszt>\n");
                out.append("\t\t<real_epr>"+inv.getRealisedRateDelta()+"</real_epr>\n");
                out.append("\t\t<real_dev>"+inv.getRealisedCurrencyRateDelta()+"</real_dev>\n");
                out.append("\t\t<kiv_kamat>"+inv.getDerecognisedInterest()+"</kiv_kamat>\n");
                out.append("\t\t<kiv_oszt>"+inv.getDerecognisedDividend()+"</kiv_oszt>\n");
                out.append("\t\t<kiv_epr>"+inv.getDerecognisedRateDelta()+"</kiv_epr>\n");
                out.append("\t\t<kiv_dev>"+inv.getDerecognisedCurrencyRateDelta()+"</kiv_dev>\n");
                out.append("\t</eszkoz>\n");
            }
            out.append("</eszkozok>");
            out.close();
        }
        catch(Exception e)
        {
            System.err.println("Írási hiba \n" + e.getMessage());
        }
        return exportXML;
    }
}
